<?php
	class ProfileController
	{
		// Membervariables
		private $_userHandler = null;
		private $_profileView = null;
		private $_profileHandler = null;
		
		public function __construct(\UserHandler $handler, \ProfileView $profileView, \ProfileHandler $profileHandler)
		{
			$this->_userHandler = $handler;
			$this->_profileView = $profileView;
			$this->_profileHandler = $profileHandler;
		}
		
		public function DoControl()
		{
			$xhtml = "";
			
			$user = $this->_userHandler->GetUser($_SESSION[UserView::USERNAME]);
				
			// Check if the user already has any options saved otherwise print the blank form.
			if ($profileHandler->GetProfile($user->id) != null)
			{
				$profile = $this->_profileHandler->GetProfile($user->id);
				$xhtml .= $this->_profileView->DoProfileOptionsForm($profile);
			}
			else 
			{
				$xhtml .= $this->_profileView->DoProfileOptionsForm(null);
			}
				
			// Check wether the user tried to save their options.
			if ($this->_profileView->TreidToSaveOptions() == true)
			{
				$profile = $this->_profileHandler->GenerateProfile($user->id);
				
				if ($this->_profileHandler->ProfilePassedValidation($profile) == true)
				{
					$this->_profileHandler->SaveProfile($profile); 
				}
			}
			
			return $xhtml;
		}
	}